package com.cjm.one.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cjm.one.goods.mapper.GoodsCollectMapper;
import com.cjm.one.goods.service.GoodsCollectService;
import com.cjm.one.model.goods.entity.GoodsCollect;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author 29289
* @description 针对表【goods_collect(用户收藏表)】的数据库操作Service实现
* @createDate 2025-03-12 21:11:07
*/
@Service
public class GoodsCollectServiceImpl extends ServiceImpl<GoodsCollectMapper, GoodsCollect>
    implements GoodsCollectService {

    @Resource
    private GoodsCollectMapper goodsCollectMapper;

    @Override
    public Boolean insertCollect(Long id, Long goodsId) {
        GoodsCollect goodsCollect = new GoodsCollect();
        goodsCollect.setCustomId(id);
        goodsCollect.setSkuId(goodsId);
        LambdaQueryWrapper<GoodsCollect> goodsCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCollectLambdaQueryWrapper.eq(GoodsCollect::getCustomId, id)
                .eq(GoodsCollect::getSkuId, goodsId);
        if (goodsCollectMapper.selectOne(goodsCollectLambdaQueryWrapper) != null) {
            return false;
        }
        return goodsCollectMapper.insert(goodsCollect)>0;
    }

    @Override
    public Boolean deleteCollect(Long customerId, Long id) {
        LambdaQueryWrapper<GoodsCollect> goodsCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCollectLambdaQueryWrapper.eq(GoodsCollect::getCustomId, customerId)
                .eq(GoodsCollect::getId, id);
        int delete = goodsCollectMapper.delete(goodsCollectLambdaQueryWrapper);
        return delete>0;
    }

    @Override
    public Boolean deleteCollectBatch(Long id, Long[] ids) {
        LambdaQueryWrapper<GoodsCollect> goodsCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCollectLambdaQueryWrapper.eq(GoodsCollect::getCustomId, id)
                .in(GoodsCollect::getId, ids);
        int delete = goodsCollectMapper.delete(goodsCollectLambdaQueryWrapper);
        return delete>0;
    }

    @Override
    public Boolean checkGoodsCollect(Long customerId, Long id) {
        LambdaQueryWrapper<GoodsCollect> goodsCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (customerId != null){
            goodsCollectLambdaQueryWrapper.eq(GoodsCollect::getCustomId, customerId);
        }

        goodsCollectLambdaQueryWrapper.eq(GoodsCollect::getSkuId, id);
        return goodsCollectMapper.selectCount(goodsCollectLambdaQueryWrapper) >0;
    }
}




